﻿Imports Entities
Imports DataAccess
Public Class FacturaBL
    Public Shared Function Create(ByVal zObject As FACTURAS) As FACTURAS
        Return FacturaDAl.Create(zObject)
    End Function

    Public Shared Function List() As List(Of FACTURAS)
        Return FacturaDAl.List
    End Function

    Public Shared Function SearchById(ByVal zId As Integer) As FACTURAS
        Return FacturaDAl.SearchById(zId)
    End Function

    Public Shared Function Save(ByVal zObject As FACTURAS, ByVal zList As List(Of DETALLEFACTURA), ByVal SinGuia As Boolean) As FACTURAS
        Try
            Dim obj = FacturaDAl.Create(zObject)
            If IsNothing(obj) Then
                Return Nothing
            End If
            For Each rec In zList
                rec.FACTURA = obj.FACTURA
                DetalleFacturaDAL.Create(rec)
                If SinGuia Then
                    ItemBL.UpdateStockMasivo(rec.ITEM, rec.CANTIDAD)
                End If
            Next
            Return obj
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Shared Function GetDetalleFacturaP(ByVal zPedidoId As Long) As List(Of DetalleFacturaP)
        Dim lista As New List(Of DETALLEPEDIDO)
        Dim listaP As New List(Of DetalleFacturaP)
        lista = PedidoDAL.ListDetallePedido(zPedidoId)

        For Each rec In lista
            Dim p As New DetalleFacturaP
            Dim item = ItemBL.SearchById(rec.ITEM)
            Dim gara = ItemBL.SearchById(item.ITEM)
            With p
                .ItemId = rec.ITEM
                .Descripcion = item.DESCRIPCION
                .GarantiasDisponibles = GarantiaBL.GetGarantiasDisponibles(gara.GARANTIA)

                .Cantidad = rec.CANTIDAD
                .PrecioUnitario = 0
                .TotalItem = 0
                .StockItem = item.STOCK
            End With
            listaP.Add(p)
        Next
        Return listaP
    End Function

    Public Shared Function GetDetalleFactura(ByVal zId As Long) As List(Of DETALLEFACTURA)
        Return DetalleFacturaDAL.ListByFacturaId(zId)
    End Function
End Class
